package com.yonyou.cyxdms.basedata.service.service.basedata;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyxdms.basedata.service.constant.BasedataCommonConstants;
import com.yonyou.cyxdms.basedata.service.dto.basedata.BasedataDTO;
import com.yonyou.cyxdms.basedata.service.entity.basedata.BasedataPO;
import com.yonyou.cyxdms.basedata.service.repository.basedata.BasedataMapper;
import com.yonyou.cyxdms.basedata.service.vo.basedata.DefinitionVO;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 基础数据 服务实现类
 * </p>
 *
 * @author dengqiang
 * @since 2019-11-21
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BasedataServiceImpl extends ServiceImpl<BasedataMapper, BasedataPO> implements BasedataService {

    @Autowired
    BasedataMapper basedataMapper;

    /**
     * 整车分类定义分页查询
     *
     * @param page
     * @param basedataType
     * @param basedataName
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.basedata.DefinitionVO>
     * @author dengqiang
     * @since 2019/11/21
     */
    @Override
    public IPage<DefinitionVO> qrySortDefinitionPage(Page<DefinitionVO> page, String basedataType, String basedataName) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        Map<String, Object> queryCondition = new HashMap<>();
        queryCondition.put(BasedataCommonConstants.APP_ID,loginInfoDto.getAppId());
        queryCondition.put(BasedataCommonConstants.OWNER_CODE,loginInfoDto.getOwnerCode());
        queryCondition.put("basedataType",basedataType);
        queryCondition.put("basedataName",basedataName);
        return basedataMapper.qrySortDefinitionPage(page,queryCondition);
    }

    /**
     * 整车分类定义修改
     *
     * @param basedataDTO
     * @return com.yonyou.cyxdms.basedata.service.entity.basedata.BasedataPO
     * @author dengqiang
     * @since 2019/11/21
     */
    @Override
    public Integer updateSortDefinition(BasedataDTO basedataDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        String basedataType = basedataDTO.getBasedataType();
        QueryWrapper<BasedataPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasedataPO::getAppId, loginInfoDto.getAppId())
                .eq(BasedataPO::getOwnerCode, loginInfoDto.getOwnerCode())
                .eq(BasedataPO::getBasedataType, basedataType)
                .ne(BasedataPO::getBasedataId, basedataDTO.getBasedataId());
        List<BasedataPO> baseDataPoList = basedataMapper.selectList(queryWrapper);
        if (!CommonUtils.isNullOrEmpty(baseDataPoList)) {
            throw new ServiceBizException("分类代码不能重复!");
        }

        BasedataPO basedataPo = basedataMapper.selectById(basedataDTO.getBasedataId());
        basedataPo.setBasedataName(basedataDTO.getBasedataName());
        basedataPo.setBasedataType(basedataDTO.getBasedataType());
        basedataPo.setDataSources(basedataDTO.getDataSources());
        basedataPo.setIsValid(basedataDTO.getIsValid());
        return basedataMapper.updateById(basedataPo);
    }

    /**
     * 整车分类定义保存
     *
     * @param basedataDto
     * @return java.lang.Integer
     * @author dengqiang
     * @since 2019/11/21
     */
    @Override
    public Integer saveSortDefinition(BasedataDTO basedataDto) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        String basedataType = basedataDto.getBasedataType();
        QueryWrapper<BasedataPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasedataPO::getAppId, loginInfoDto.getAppId())
                .eq(BasedataPO::getOwnerCode, loginInfoDto.getOwnerCode())
                .eq(BasedataPO::getBasedataType, basedataType);
        List<BasedataPO> baseDataPoList = basedataMapper.selectList(queryWrapper);
        if (!CommonUtils.isNullOrEmpty(baseDataPoList)) {
            throw new ServiceBizException("分类代码不能重复!");
        }

        BasedataPO basedataPo = new BasedataPO();
        basedataPo.setBasedataName(basedataDto.getBasedataName());
        basedataPo.setBasedataType(basedataType);
        basedataPo.setDataSources(basedataDto.getDataSources());
        basedataPo.setIsValid(basedataDto.getIsValid());
        return basedataMapper.insert(basedataPo);
    }

    /**
     * 整车分类定义详情信息
     *
     * @param basedataId
     * @return com.yonyou.cyxdms.basedata.service.vo.basedata.DefinitionVO
     * @author dengqiang
     * @since 2019/11/21
     */
    @Override
    public DefinitionVO qrySortDefinitionDetail(Long basedataId) {
        BasedataPO basedataPo = basedataMapper.selectById(basedataId);
        DefinitionVO definitionVO = new DefinitionVO();
        definitionVO.setBasedataId(basedataPo.getBasedataId());
        definitionVO.setBasedataType(basedataPo.getBasedataType());
        definitionVO.setBasedataName(basedataPo.getBasedataName());
        definitionVO.setDataSources(basedataPo.getDataSources());
        definitionVO.setIsValid(basedataPo.getIsValid());
        return definitionVO;
    }
}
